/**
  ******************************************************************************
  * File Name          : innerprotocol.c
  * Description        : This file provides code for the configuration
  *                      of the lora e22-400t22s instances.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */
#include "lora.h"
#include "tskcfg.h"
#include "sensor.h"
#include "bsp.h"
#include <string.h>

static Collector_DataInterfaceType collector_data_interface;

volatile SensorType SENSOR = 
{
	.Status_Pow = 0,
  
	.temperature = 0.0,
	.Status_T = 0,
  
	.humidity = 0.0,
	.Status_H = 0,
  
  .nh3 = 0,
	.Status_NH3 = 0,

	.co2 = 0,
	.Status_CO2 = 0,

	.tvoc = 0,
	.Status_TVOC = 0,
  
  .pressure = 0.0,
	.Status_Pres = 0,
};

uint8_t LoRa_ReceiveDeal(uint8_t srcid, uint8_t *payload, uint8_t pllen)
{
	uint16_t sensor_status = 0x0000;
  
  if(srcid != 0)
	{
		return 1;
	}
  
	if(payload[0] != 0x5au)
	{
		return 2;
	}
  
	if(pllen != 1)
	{
		return 4;
	}
	
	if(SENSOR.Status_Pow)
		sensor_status |= COLLECTOR_DATA_FLAG_POWR;
	else
		sensor_status &= ~COLLECTOR_DATA_FLAG_POWR;
  
	if(SENSOR.Status_T)
		sensor_status |= COLLECTOR_DATA_FLAG_TEMP;
	else
		sensor_status &= ~COLLECTOR_DATA_FLAG_TEMP;
  
	if(SENSOR.Status_H)
		sensor_status |= COLLECTOR_DATA_FLAG_HUMI;
	else
		sensor_status &= ~COLLECTOR_DATA_FLAG_HUMI;
  
	if(SENSOR.Status_NH3)
		sensor_status |= COLLECTOR_DATA_FLAG_NH3;
	else
		sensor_status &= ~COLLECTOR_DATA_FLAG_NH3;
  
	if(SENSOR.Status_CO2)
		sensor_status |= COLLECTOR_DATA_FLAG_CO2;
	else
		sensor_status &= ~COLLECTOR_DATA_FLAG_CO2;
  
	if(SENSOR.Status_TVOC)
		sensor_status |= COLLECTOR_DATA_FLAG_TVOC;
	else
		sensor_status &= ~COLLECTOR_DATA_FLAG_TVOC;
  
	if(SENSOR.Status_Pres)
		sensor_status |= COLLECTOR_DATA_FLAG_PRES;
	else
		sensor_status &= ~COLLECTOR_DATA_FLAG_PRES;

	collector_data_interface.flag        = sensor_status;
	collector_data_interface.temperature = (int16_t)(SENSOR.temperature*10);
	collector_data_interface.humidity    = (uint8_t)SENSOR.humidity;
	collector_data_interface.nh3         = (uint8_t)SENSOR.nh3;
	collector_data_interface.tvoc        = SENSOR.tvoc;
	collector_data_interface.co2         = SENSOR.co2;
	collector_data_interface.pressure    = SENSOR.pressure;
	
	LoRa_LinkSend(0, (uint8_t *)&collector_data_interface, sizeof(Collector_DataInterfaceType));
	
	return 0;
}
